package quick.start.core.extend;

import android.content.Context;
import android.view.Gravity;
import android.widget.Toast;

/**
 * Toast������,���ڶ�Toast���и����Զ������,�������ԭ���Toast��
 * @author ���ӱ
 * @version 1.0.0
 * @date 2013-6-23
 *
 */
public class ToastExtend {

	private Toast toast;
	private int bgColor = 0xcc1d3566;
	private static Context mContext;
	private static ToastExtend instance;
	
	protected ToastExtend(Context context){
		toast = new Toast(context);
		toast.getView().setBackgroundColor(bgColor);
		toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
		toast.setText("");
		toast.setDuration(Toast.LENGTH_SHORT);
	}
	
	public static ToastExtend instance(Context context){
		if(null == instance){
			instance = new ToastExtend(context);
		}else{
			mContext = context; 
		}
		return instance;
	}
	
	public ToastExtend setBGColor(int color){
		bgColor = color;
		if(null != toast){
			toast.getView().setBackgroundColor(bgColor);
		}
		return this;
	}
	
	public ToastExtend setText(String message){
		if(null != toast){
			toast.setText(message);
		}
		return this;
	}
	
	public ToastExtend setText(int resourceId){
		String str = mContext.getResources().getString(resourceId);
		if(str.isEmpty()){
			str = "resource id=" + String.valueOf(resourceId) + " not exists";
		}
		toast.setText(str);
		return this;
	}
	
	public ToastExtend setSize(int width, int height){
		if(null != toast){
			toast.setGravity(Gravity.CENTER, width, height);
		}
		return this;
	}
	
	public void show(){
		show(Toast.LENGTH_SHORT);
	}
	
	public void show(int duration){
		toast.setDuration(duration);
		toast.show();
	}
	
	public void clear(){
		if(null != toast){
			toast.cancel();
		}
	}
}
